app_family.controller("controller_familyinfo", function($scope, $rootScope, httpRequest) {
	$rootScope.$broadcast('currentModule', 'page_familyinfo');
	$rootScope.$broadcast('footerFlag', false); // 是否显示底部导航
	
	/**
	 * 判断是否用手机登录过
	 */
	if (sessionStorage.getItem('data_sessionStorage')) {
		var familyPhone = JSON.parse(sessionStorage.getItem('data_sessionStorage')).familyInfo.phone;
		if (familyPhone == '' || familyPhone == null) {
			window.location.replace(basepath + 'login.html');
			return;
		}
	} else {
		window.location.replace(basepath + 'login.html');
		return;
	}
	
	$scope.isViewFlag = true;
	$scope.showController = {
		nameShow : false,
		sexShow : false,
		idCardNoShow : false,
		mobileShow : false,
		mobileAddress : false,
		areaShow : false
	};


	// 根据家属手机号，查询家属个人数据
	httpRequest.getMineInfo({
		mobile : familyPhone
	}, function (data) {
		if (data.success === true) {
			$scope.familyInfoObj = data.obj;
			
			$scope.avator_sex = imagebasepath + data.obj.icon; // 头像
		} else if (data.success === false) {
			$.dialog.showToast({msgTxt : data.msg});
		}
	});
	
	/**
	 * 点击修改个人信息
	 */
	$scope.showMofy = function (type) {
		$scope.modyType = type;
		$scope.isViewFlag = false;
		$scope.isModyFlag = true;
		$scope.isModyAreaFlag = false;
		
		if (type === 'user_name') {
			$scope.showController = {
				nameShow : true,
				sexShow : false,
				idCardNoShow : false,
				mobileShow : false,
				mobileAddress : false
			};
			return;
		}
		
		if (type === 'sex_name') {
			$scope.showController = {
				nameShow : false,
				sexShow : true,
				idCardNoShow : false,
				mobileShow : false,
				mobileAddress : false
			};
			return;
		}
		
		if (type === 'id_card_no') {
			$scope.showController = {
				nameShow : false,
				sexShow : false,
				idCardNoShow : true,
				mobileShow : false,
				mobileAddress : false
			};
			return;
		}
		
		if (type === 'mobile') {
			$scope.showController = {
				nameShow : false,
				sexShow : false,
				idCardNoShow : false,
				mobileShow : true,
				mobileAddress : false
			};
			return;
		}
		
		if (type === 'mobileAddress') {
			$scope.showController = {
				nameShow : false,
				sexShow : false,
				idCardNoShow : false,
				mobileShow : false,
				mobileAddress : true
			};
			return;
		}
	};
	
	/**
	 * 所在地区点击
	 */
	$scope.areaSel = function () {
		$scope.isViewFlag = false;
		$scope.isModyFlag = false;
		$scope.isModyAreaFlag = true;
		
		$scope.chooseProvince = true;
		$scope.chooseCity = false;
		$scope.chooseCountry = false;
		
		// 获取省市
		httpRequest.getProvinceAndCity('', function (data) {
			$scope.provinceList = data.obj;
		});
	};
	
	/**
	 * 省份点击
	 */
	$scope.provinceClick = function (index, code, name) {
		$scope.chooseCity = true;                  // 显示城市
		$scope.chooseProvince = false;             // 隐藏省份
		$scope.chooseCountry = false;
		$scope.citys = $scope.provinceList[index].child;
		
		$scope.tmp_province_code = code;        // 暂时先保存省份code
		$scope.tmp_province_name = name;        // 暂时先保存省份name
	};
	
	/**
	 * 城市点击
	 */
	$scope.cityClick = function (code, name) {
		httpRequest.getCountyByCity({
			code : code
		}, function (data) {
			$scope.chooseCountry = true;           // 显示区域
			$scope.chooseCity = false;             // 隐藏城市
			$scope.chooseProvince = false;         // 隐藏省份
			$scope.countrys = data.obj;
			
			$scope.tmp_city_code = code;        // 注册用城市
			$scope.tmp_city_name = name;        // 注册用城市
		});
	};
	
	/**
	 * 区县点击
	 */
	$scope.countryClick = function (code, name) {
		$scope.tmp_country_code = code;        // 注册用区县
		$scope.tmp_country_name = name;        // 注册用区县
		
		$scope.familyInfoObj.province = $scope.tmp_province_code;
		$scope.familyInfoObj.province_name = $scope.tmp_province_name;
		$scope.familyInfoObj.city = $scope.tmp_city_code;
		$scope.familyInfoObj.city_name = $scope.tmp_city_name;
		$scope.familyInfoObj.county = $scope.tmp_country_code;
		$scope.familyInfoObj.county_name = $scope.tmp_country_name;
		
		// 修改家属个人信息
		httpRequest.updateMineInfo($scope.familyInfoObj, function (data) {
			if (data.success === true) {
				$scope.isViewFlag = true;
				$scope.isModyFlag = false;
				$scope.isModyAreaFlag = false;
				$scope.chooseCountry = false;                  // 隐藏区县
				
				$.dialog.showToast({
					msgTxt : data.msg,
					callbackOk : function () {
						$scope.familyInfoObj = data.obj;
					}
				});
			} else if (data.success === false) {
				$.dialog.showToast({msgTxt : data.msg});
			}
		});
	};

	/**
	 * 性别选择
	 */
	$scope.sexClick = function (sexCode) {
		if (sexCode == '2') { // 女性
			$scope.familyInfoObj.sex_name = "女";
			$scope.familyInfoObj.sex = "2";
		} else { // 男性
			$scope.familyInfoObj.sex_name = "男";
			$scope.familyInfoObj.sex = "1";
		}
	};
	
	/**
	 * 生日修改
	 */
	$('#modifyBirthday').mobiscroll().calendar({
        theme: 'mobiscroll',  
        lang: 'zh', 
        display: 'bottom',   
        controls: ['date'],
        dateFormat : 'yy-mm-dd',
        maxDate : new Date(),
        showNow : false,
        setText: '',
        headerText : '生日',
        layout : 'fixed',
        setText: '确定',
        cancelText: '取消',
        onBeforeShow: function (inst) {
        },
        onSelect: function (valueText, inst) {
        	$scope.familyInfoObjOld = {};
        	$scope.familyInfoObjOld = angular.copy($scope.familyInfoObj);
        	$scope.familyInfoObjOld.birthday = valueText;
        	
        	// 修改家属个人信息
    		httpRequest.updateMineInfo($scope.familyInfoObjOld, function (data) {
    			if (data.success === true) {
    				$scope.isViewFlag = true;
    				$scope.isModyFlag = false;
    				$scope.isModyAreaFlag = false;
    				
    				$scope.familyInfoObj = data.obj;
    				$.dialog.showToast({msgTxt : data.msg});
    			} else if (data.success === false) {
    				$.dialog.showToast({msgTxt : data.msg});
    			}
    		});
        }
	});
	
	/**
	 * 清空文本框内的文本
	 */
	$scope.clearTxt = function (str) {
    	if (str === 'user_name') { // 姓名
    		$scope.familyInfoObj.user_name = '';
    		document.getElementById("userName").focus();
    	} else if (str === 'id_card_no') { // 身份证号
    		$scope.familyInfoObj.id_card_no = '';
    		document.getElementById("idCardNo").focus();
    	} else if (str === 'address') { // 详细地址
    		$scope.familyInfoObj.address = '';
    		document.getElementById("address").focus();
    	}
    };
	
	/**
	 * 修改家属个人信息 返回
	 */
	$scope.modyReturn = function () {
		$scope.isViewFlag = true;
		$scope.isModyFlag = false;
		$scope.isModyAreaFlag = false;
		
		// 根据家属手机号，查询家属个人数据
		httpRequest.getMineInfo({
			mobile : familyPhone
		}, function (data) {
			if (data.success === true) {
				$scope.familyInfoObj = data.obj;
			} else if (data.success === false) {
				$.dialog.showToast({msgTxt : data.msg});
			}
		});
	};
	
	/**
	 * 保存家属个人信息
	 */
	$scope.saveFamilyInfo = function () {
		// 前端校验
		if ($scope.modyType === 'user_name') {
			if(null == $scope.familyInfoObj.user_name || "" == $scope.familyInfoObj.user_name) {
				$.dialog.showToast({
					msgTxt : '请输入名字。',
					callbackOk : function () {
						document.getElementById("userName").focus();
					}
				});
				return;
			}
			if(reg_specialchar.test($scope.familyInfoObj.user_name)) {
				$.dialog.showToast({
					msgTxt : "您的名字里不能包含" + tip_specialchar + "能特殊字符。",
					callbackOk : function () {
						document.getElementById("userName").focus();
					}
				});
				return;
			}
		}
		
		if ($scope.modyType === 'id_card_no') {
			if(null == $scope.familyInfoObj.id_card_no || "" == $scope.familyInfoObj.id_card_no) {
				$.dialog.showToast({
					msgTxt : '请输入身份证号。',
					callbackOk : function () {
						document.getElementById("idCardNo").focus();
					}
				});
				return;
			}
			if($scope.familyInfoObj.id_card_no.length != 15 && $scope.familyInfoObj.id_card_no.length != 18) {
				$.dialog.showToast({
					msgTxt : '身份证长度为15位或者18位。',
					callbackOk : function () {
						document.getElementById("idCardNo").focus();
					}
				});
				return;
			}
			if(reg_specialchar.test($scope.familyInfoObj.id_card_no)) {
				$.dialog.showToast({
					msgTxt : "您的身份证里不能包含" + tip_specialchar + "能特殊字符。",
					callbackOk : function () {
						document.getElementById("idCardNo").focus();
					}
				});
				return;
			}
		}
		
		if ($scope.modyType === 'mobileAddress') {
			if(null == $scope.familyInfoObj.address || "" == $scope.familyInfoObj.address) {
				$.dialog.showToast({
					msgTxt : '请输入详细的地址。',
					callbackOk : function () {
						document.getElementById("address").focus();
					}
				});
				return;
			}
			if(reg_specialchar.test($scope.familyInfoObj.address)) {
				$.dialog.showToast({
					msgTxt : "您的地址里不能包含" + tip_specialchar + "能特殊字符。",
					callbackOk : function () {
						document.getElementById("address").focus();
					}
				});
				return;
			}
		}
		
		if ($scope.modyType === 'sex_name') {
			if(null == $scope.familyInfoObj.sex || "" == $scope.familyInfoObj.sex) {
				$.dialog.showToast({
					msgTxt : '请选择性别。'
				});
				return;
			}
		}
		
		// 修改家属个人信息
		httpRequest.updateMineInfo($scope.familyInfoObj, function (data) {
			if (data.success === true) {
				$scope.isViewFlag = true;
				$scope.isModyFlag = false;
				$scope.isModyAreaFlag = false;
				$.dialog.showToast({
					msgTxt : data.msg,
					callbackOk : function () {
	    				$scope.familyInfoObj = data.obj;
					}
				});
			} else if (data.success === false) {
				$.dialog.showToast({msgTxt : data.msg});
			}
		});
	};
	
	/**
	 * 修改头像
	 */
	var appElement = document.querySelector('[ng-controller=controller_familyapp]');
    var loadingScope = angular.element(appElement).scope();
	// 头像点击
    $("#logox").click(function() {
        $(".htmleaf-container").show();
    });
	// 裁剪图片区域
    $("#clipArea").photoClip({
        width: 220,
        height: 220,
        file: "#file",
        view: "#view",
        ok: "#clipBtn",
        loadStart: function() {
            //console.log("照片读取中");
            loadingScope.loading = true;
            loadingScope.$apply();
        },
        loadComplete: function() {
            //console.log("照片读取完成");
            loadingScope.loading = false;
            loadingScope.$apply();
        },
        clipFinish: function(dataURL) {
        }
    });
    
    // 选取
    $("#clipBtn").click(function() {
        if (imgsource != '' && imgsource != null) {
        	var optionStr = imgsource.slice(23); // imgsource变量在jQuery-photoClip中定义  用canvas中的toDataURL将画布转换成base64编码的image
            var url = basepath + 'uploadIcon';
            
            httpRequest.updateMineIcon({
        		mobile : familyPhone,
        		icon : optionStr
        	}, function (data) {
        		if (data.success === true) {
        			$scope.avator_sex = serverIp + '/cloudbox.resource' + data.obj.icon;
        			$.dialog.showToast({msgTxt : data.msg});
        		} else if (data.success === false) {
        			$.dialog.showToast({msgTxt : data.msg});
        		}
        	});
            
            $(".htmleaf-container").hide();
        }
    });
    // 取消按钮
    $scope.upload_cancel = function () {
    	$(".htmleaf-container").hide();
    };
});